<?php
namespace jy_running;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class worker extends \plugin{
	/*
		系统指派
	*/
	public function syspai($order=array(),$nodriver_id=0){
		$setting=$this->plugin_setting('setting');
		if($setting['open_syspai']!=1){
			return false;
		}
		$dd=ijson_encode($order);
		$drivers=pdo_getall('jy_running_driver',array('id <>'=>$nodriver_id,'uniacid'=>$_SESSION['uniacid'],'busy'=>1,'status'=>1,'stop'=>0));	
		$order['lat']=json_decode($order['lat'],true);
		$fanwei=intval($setting['fanwei'])?:30;
		foreach($drivers as $k=>&$v){
			$v['latlng']=json_decode($v['latlng'],true);
			$v['juli']=cfc('tools')->getdistance($order['lat']['lng'],$order['lat']['lat'],$v['latlng']['lng'],$v['latlng']['lat']);
			$v['juli']=intval($v['juli']/1000);
			if($v['juli']>$fanwei){
				unset($drivers[$k]);
			}
		}
		paixu($drivers,'juli');
		$driverid=$drivers['0']['id'];
		if(!$driverid){
			return false;
		}
		$params=array(
			'mid'=>$order['mid'],
			'message'=>'您的《'.$order['start_seat'].'~~'.$order['dest_seat'].'》订单已系统指派！',
		);
		$this->model('notice')->add_msg($params);
		pdo_update('jy_running_order',array('status'=>6,'on6_time'=>TIMESTAMP,'driver_id'=>$driverid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		pdo_update('jy_running_driver',array('busy'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$driverid));
		$order['lat']=json_decode($order['lat'],true);
		$message=ijson_encode($order);
		$res=cfc('jinyun_socket')->send2uid($message,$drivers['0']['mid']);
		return $driverid;
	}
	/*
		快跑员接单
	*/
	public function getpai($order){
		$setting=$this->plugin_setting('setting');
		$mid=$order['mid'];
		$driver=pdo_get('jy_running_driver',array('mid'=>$_SESSION['uid']));
		$params=array(
			'mid'=>$order['mid'],
			'message'=>'您的《'.$order['start_seat'].'~~'.$order['dest_seat'].'》订单快跑员已接到！',
		);
		$this->model('notice')->add_msg($params);
		pdo_update('jy_running_order',array('driver_id'=>$driver['id'],'status'=>1),array('id'=>$order['id']));
		$order['lat']=json_decode($order['lat'],true);
		$message=ijson_encode($order);
		cfc('jinyun_socket')->send2uid($message,$mid);
		return $driver['id'];
	}
	/*
		获取订单，快跑员确认后才算接单
	*/
	public function getorder($order){
		$setting=$this->plugin_setting('setting');
		$driver=pdo_get('jy_running_driver',array('mid'=>$_SESSION['uid']));
		if(!$driver['id']){
			return false;
		}
		pdo_update('jy_running_order',array('driver_id'=>$driver['id'],'on6_time'=>TIMESTAMP,'status'=>6),array('id'=>$order['id']));
		$message=ijson_encode($order);
		return $driver['id'];
	}
	/*
		拒绝
	*/
	public function nopai($order){
		$setting=$this->plugin_setting('setting');
		if($order['nolist']){
			$nolist=json_decode($order['nolist'],true);
			$nolist[]=$_SESSION['uid'];
		}else{
			$nolist=array($_SESSION['uid']);
		}
		$no=json_encode($nolist);
		pdo_update('jy_running_order',array('nolist'=>$no),array('id'=>$order['id']));
		$driverid=$this->syspai($order);
		if(!$driverid){
				pdo_update('jy_running_order',array('status'=>0),array('id'=>$order['id']));
		}
		$info=pdo_get('jy_running_driver',array('mid'=>$_SESSION['uid']));
		if($info['notimes']>=($setting['black']-1)){
				pdo_update('jy_running_driver',array('busy'=>1,'notimes +'=>1,'stop'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));		
		}else{
				pdo_update('jy_running_driver',array('busy'=>1,'notimes +'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));		
		}
		return $driverid;
	}
	public function getworkers($lat_lng=array(),$maxjuli=0,$limit=0,$page=0){
		$setting=$this->plugin_setting('setting');
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid <>'=>$_SESSION['uid'],
			'status'=>1,
			'busy'=>1,
			'stop'=>0,
			'latlng <>'=>'',
		);
		$maxjuli=intval($maxjuli)?:$setting['fanwei'];
		$maxjuli=$maxjuli?:30;
		$pindex = max(1, intval($page));
		$psize = 500;
		$total='';
		$list=pdo_getslice('jy_running_driver',$params,array($pindex,$psize),$total,array(),'','createtime desc');
		foreach($list as $k=>&$v){
			$v['latlng']=json_decode($v['latlng'],true);
			$v['nowjuli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['latlng']['lng'],$v['latlng']['lat']);
			$v['nowjuli']=intval($v['nowjuli']/1000);
			if($maxjuli && $maxjuli<$v['nowjuli']){
				unset($list[$k]);
			}
		}
		paixu($list,'nowjuli');
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=intval($page)?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function check_busy($driver_id,$checkother=false,$isexi=false){
		$order=pdo_getall('jy_running_order',array('driver_id'=>$driver_id,'status'=>array(1,6,7,8)));
		if(count($order)==0){
			pdo_update('jy_running_driver',array('busy'=>1),array('busy'=>2,'mid'=>$_SESSION['uid']));
		}else{
			pdo_update('jy_running_driver',array('busy'=>2),array('busy'=>1,'mid'=>$_SESSION['uid']));
			if($isexi){
				exi('您还有未处理完的订单！前往查看！','error','/worker_order/');
			}else{
				return false;
			}
		}
		if($checkother){
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit1','credit2'));
			if($member['credit2']<20){
				if($isexi){
					exi('余额不足20，不能接单','error','/home/');
				}else{
					return false;
				}
			}
		}
		return true;
	}
	/*
		更新在线时间
	*/
	public function up_dayonlinetime(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$timelog=pdo_get('jy_running_driver_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime),array('id'));
		$lastontime=pdo_getcolumn('jy_running_driver',array('mid'=>$_SESSION['uid']),'lastontime');
		if($timelog['id'] && $lastontime){
			pdo_update('jy_running_driver_log',array('dayonlinetime +='=>TIMESTAMP-$lastontime),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime));
		}elseif($lastontime){
			$log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'dayonlinetime'=>TIMESTAMP-$lastontime,
			);
			$log['createtime']=TIMESTAMP;
			pdo_insert('jy_running_driver_log',$log);
		}
		return true;
	}
}